import I18n from 'i18n-js';
import { toJS, makeAutoObservable } from 'mobx';
import ToastContainer from '@/utils/Toast';
import { accountInfo, changeInfo, updateUserInfo, updateEmailInfo, modifyPwd } from '@/services/account';
import logout from "@/utils/logout";
import { getWechatId } from '@/utils/loginStatus'

interface IUserInfo {
  address?: string;
  avatar?: string;
  nickname?: string;
  gender?: string;
  mobile?: string;
  email?: string;
  lang?: string;
  hasPass?: boolean;
  openid?: string;
}

interface IMobileInfo {
  mobile: string;
  code: string;
}

interface IEMailInfo {
  mail: string;
  code: string;
}

class UserState {

  userInfo: IUserInfo = {};

  constructor() {
    makeAutoObservable(this);
  }

  async getUserInfo() {
    const data = await accountInfo();
    if (data.success) {
      this.userInfo = { ...data.data } as IUserInfo;
      // this.checkWechatIdIsBind();
    }
    return data as IUserInfo;
  }

  async checkWechatIdIsBind() {
    const wechatId = await getWechatId();
    if (wechatId && wechatId.length > 0) {
      // 有id才判断
      if (!this.userInfo.openid) {
        console.log('检测到微信与账号解绑了')
        logout();
        return;
      }
      if (this.userInfo.openid && this.userInfo.openid !== wechatId) {
        console.log('检测到微信与账号解绑了2')
        logout();
        return;
      }
    }
  }

  async changeUserInfo(data: IUserInfo, success?: any, error?: any) {
    const { address, avatar, nickname, gender, mobile, email } = this.userInfo;
    const obj = {
      address,
      avatar,
      nickname,
      gender,
      mobile,
      email,
      ...data,
    };
    const res = await changeInfo(obj);
    if (res.success) {
      this.userInfo = obj;
      console.log('userInfo', toJS(obj));
    }
    return res;
  }

  async updateUserInfo(
    data: IMobileInfo,
    token?: string
  ) {
    const res = await updateUserInfo(data, token);
    if (res.success) {
      this.userInfo.mobile = data.mobile;
    }
    return res;
  }

  async updateEmail(data: IEMailInfo, success: any, error: any) {
    const res = await updateEmailInfo(data);
    if (res.success) {
      this.userInfo.email = data.mail;
      success && success(res);
    } else if (error) {
      error(res);
    }
  }

  async updatePassword(params: { newPassword: string; oldPassword?: string }) {
    const res: IResult = await modifyPwd(params);
    if (res.success) {
      ToastContainer.success(I18n.t("UPDATE_SUCCESS"));
    } else {
      switch (parseInt(`${res.code}`, 10)) {
        case 1000033:
          ToastContainer.error(I18n.t("OLD_PASS_ERROR"));
          break;
        case 504:
          ToastContainer.error(I18n.t("PASSWORD_STYLE_ERROR"));
          break;
        default:
          ToastContainer.error(I18n.t("CHANGE_PASSWORD_ERROR"));
      }
    }
    return res
  }

  getNickName() {
    if (this.userInfo) {
      return this.userInfo.nickname || this.userInfo.mobile;
    }
    return '';
  }

  cleanData() {
    console.log('userInfo cleanData');
    this.userInfo = {};
  }
}

const UserState_Store_Key = "userState";

export { UserState, UserState_Store_Key };


export default new UserState();
